﻿using System;
using StudentManagement.Data;
using System.Collections.Generic;
using System.Data;
using System.Data.SQLite;
namespace StudentManagement.Services
{
    public class SchoolService
    {
        private readonly DatabaseHelper _db;

        public SchoolService()
        {
            _db = new DatabaseHelper();
        }

        public void AddSchool(string name)
        {
            string query = "INSERT INTO School (Name) VALUES (@name)";
            _db.ExecuteNonQuery(query, new SQLiteParameter("@name", name));
            _db.LogOperation($"添加学校：{name}");
        }

        public void UpdateSchool(int id, string name)
        {
            string query = "UPDATE School SET Name = @name WHERE Id = @id";
            _db.ExecuteNonQuery(query, new SQLiteParameter("@name", name), new SQLiteParameter("@id", id));
            _db.LogOperation($"修改学校 ID={id} 名称为：{name}");
        }

        public void DeleteSchool(int id)
        {
            string query = "DELETE FROM School WHERE Id = @id";
            _db.ExecuteNonQuery(query, new SQLiteParameter("@id", id));
            _db.LogOperation($"删除学校 ID={id}");
        }

        public List<SchoolInfo> GetAllSchools()
        {
            string query = "SELECT * FROM School";
            DataTable dt = _db.ExecuteQuery(query);

            var list = new List<SchoolInfo>();
            foreach (DataRow row in dt.Rows)
            {
                list.Add(new SchoolInfo
                {
                    Id = Convert.ToInt32(row["Id"]),
                    Name = row["Name"].ToString()
                });
            }
            return list;
        }
    }
    public class ClassService
    {
        private readonly DatabaseHelper _db;

        public ClassService()
        {
            _db = new DatabaseHelper();
        }

        public void AddClass(string name, int schoolId)
        {
            string query = "INSERT INTO Class (Name, SchoolId) VALUES (@name, @schoolId)";
            _db.ExecuteNonQuery(query,
                new SQLiteParameter("@name", name),
                new SQLiteParameter("@schoolId", schoolId));
            _db.LogOperation($"添加班级：{name}, 所属学校 ID={schoolId}");
        }

        public void UpdateClass(int id, string name, int schoolId)
        {
            string query = "UPDATE Class SET Name = @name, SchoolId = @schoolId WHERE Id = @id";
            _db.ExecuteNonQuery(query,
                new SQLiteParameter("@name", name),
                new SQLiteParameter("@schoolId", schoolId),
                new SQLiteParameter("@id", id));
            _db.LogOperation($"修改班级 ID={id} 为：{name}, 学校 ID={schoolId}");
        }

        public void DeleteClass(int id)
        {
            string query = "DELETE FROM Class WHERE Id = @id";
            _db.ExecuteNonQuery(query, new SQLiteParameter("@id", id));
            _db.LogOperation($"删除班级 ID={id}");
        }

        public List<ClassInfo> GetAllClasses()
        {
            string query = "SELECT * FROM Class";
            DataTable dt = _db.ExecuteQuery(query);

            var list = new List<ClassInfo>();
            foreach (DataRow row in dt.Rows)
            {
                list.Add(new ClassInfo
                {
                    Id = Convert.ToInt32(row["Id"]),
                    Name = row["Name"].ToString(),
                    SchoolId = Convert.ToInt32(row["SchoolId"])
                });
            }
            return list;
        }
    }
    public class StudentManager
    {
        private readonly DatabaseHelper db = new DatabaseHelper();

        public void AddStudent(string name, int classId)
        {
            string query = "INSERT INTO Student (Name, ClassId) VALUES (@name, @classId)";
            db.ExecuteNonQuery(query,
                new SQLiteParameter("@name", name),
                new SQLiteParameter("@classId", classId));
            db.LogOperation($"添加学生: {name}, 班级ID: {classId}");
        }

        public DataTable GetAllStudents()
        {
            string query = "SELECT * FROM Student";
            return db.ExecuteQuery(query);
        }

        public void UpdateStudent(int id, string name)
        {
            db.ExecuteNonQuery("UPDATE Student SET Name = @name WHERE Id = @id",
                new SQLiteParameter("@name", name),
                new SQLiteParameter("@id", id));
            db.LogOperation($"更新学生: ID = {id}, 新姓名: {name}");
        }

        public void DeleteStudent(int id)
        {
            db.ExecuteNonQuery("DELETE FROM Student WHERE Id = @id",
                new SQLiteParameter("@id", id));
            db.LogOperation($"删除学生: ID = {id}");
        }

        public DataTable GetLogs()
        {
            return db.ExecuteQuery("SELECT * FROM Log ORDER BY Timestamp DESC");
        }
    }
}
